/// 点云生成
/// 显示三角形和一个4边形
///
#include <vtkSmartPointer.h>
#include <vtkPolygon.h>
#include <vtkTriangle.h>
#include <vtkCellArray.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkProperty.h>
#include "vtkTriangleFilter.h"

#include <iostream>
using namespace std;

static void displayVtkData(char * title, vtkSmartPointer<vtkMapper> mapper);

static vector<vector<double>> pre_points = {
    {-9.554985, -248.238281, 2172.197021}, {57.472542, -252.039856, 2186.527832}, {-6.665657, -322.107086, 2166.473389}, {69.484123, -333.846558, 2182.775879},
    {-19.533240, -248.223526, 2172.066406}, {57.468151, -252.037445, 2186.596436}, {-6.654734, -322.104980, 2166.449707}, {69.492149, -333.830750, 2182.679688}
};

///
/// \brief 演示如何建立一个图形，如何使用vtkTriangleFilter 把输入数据全部转成三角形
/// \param argc
/// \param argv
/// \return
///
int main(int argc, char *argv[])
{
    vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
    vtkSmartPointer<vtkCellArray> vertices = vtkSmartPointer<vtkCellArray>::New();
    for(int i=0; i<pre_points.size(); i++) {
        points->InsertPoint(i, pre_points[i].data());
        vertices->InsertNextCell(1);
        vertices->InsertCellPoint(i);
    }

    vtkSmartPointer<vtkPolyData> polygonPolyData =
        vtkSmartPointer<vtkPolyData>::New();
    polygonPolyData->SetPoints(points);
    polygonPolyData->SetVerts(vertices);


    vtkSmartPointer<vtkPolyDataMapper> mapper =
        vtkSmartPointer<vtkPolyDataMapper>::New();
    mapper->SetInputData(polygonPolyData);

    displayVtkData("point clouds", mapper);

    return EXIT_SUCCESS;
}

static void displayVtkData(char * title, vtkSmartPointer<vtkMapper> mapper)
{
    vtkSmartPointer<vtkActor> actor =
        vtkSmartPointer<vtkActor>::New();
    actor->SetMapper(mapper);
    actor->GetProperty()->SetColor(1, 0, 0);
    actor->GetProperty()->SetPointSize(4.0);
    //actor->GetProperty()->SetRepresentationToWireframe();

    vtkSmartPointer<vtkRenderer> renderer =
        vtkSmartPointer<vtkRenderer>::New();
    renderer->AddActor(actor);
    renderer->SetBackground(0.5, 0.5, 0.5);

    vtkSmartPointer<vtkRenderWindow> renderWindow =
        vtkSmartPointer<vtkRenderWindow>::New();
    renderWindow->AddRenderer(renderer);
    renderWindow->SetSize( 640, 480 );
    renderWindow->Render();
    renderWindow->SetWindowName(title);


    vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
        vtkSmartPointer<vtkRenderWindowInteractor>::New();
    renderWindowInteractor->SetRenderWindow(renderWindow);

    renderWindow->Render();
    renderWindowInteractor->Start();
}

